#!/bin/bash
echo -e  "\e[32m=======开始安装nginx========\e[0m"
if ! docker images | grep "nginx.*latest.*" &>/dev/null
then
	
	echo -e  "\e[33m=======开始拉取redis镜像========\e[0m"
docker pull nginx
	echo -e  "\e[33m=======镜像拉取完成========\e[0m"
fi
if docker ps -a | grep "nginx$" &>/dev/null
then
	docker rm -f  nginx &>/dev/null
fi
# 启动一个nginx，把配置文件拷贝出来，在删除容器
if ! [ -f /mydata/nginx/conf/conf.d/default.conf ]
then
echo "************************************"
docker run -p80:80 --name nginx -d nginx >/dev/null
mkdir -p /mydata/nginx/html
mkdir -p /mydata/nginx/logs
mkdir -p /mydata/nginx/conf 
docker container cp nginx:/etc/nginx /mydata/nginx/conf >/dev/null
mv /mydata/nginx/conf/nginx/* /mydata/nginx/conf/ >/dev/null
rm -rf /mydata/nginx/conf/nginx >/dev/null
docker stop nginx >/dev/null
docker rm nginx >/dev/null
fi
# 运行真正的容器
docker run -p 80:80 --name nginx \
 -v /mydata/nginx/html:/usr/share/nginx/html \
 -v /mydata/nginx/logs:/var/log/nginx \
 -v /mydata/nginx/conf/:/etc/nginx \
 -d nginx >/dev/null
echo -e "\e[32m======nginx安装完成========\e[0m"
